package com.lixinkeji.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

public class DateUtils {

    /**
     * 返回unix时间戳 (1970年至今的秒数)
     *
     * @return
     */
    public static long getUnixStamp() {
        return System.currentTimeMillis() / 1000;
    }

    /**
     * 得到昨天的日期
     *
     * @return
     */
    public static String getYestoryDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String yestoday = sdf.format(calendar.getTime());
        return yestoday;
    }

    /**
     * 得到今天的日期
     *
     * @return
     */
    public static String getTodayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        return date;
    }

    /**
     * 时间戳转化为时间格式
     *
     * @param timeStamp
     * @return
     */
    public static String timeStampToStr(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(timeStamp * 1000);
        return date;
    }

    /**
     * 得到日期   yyyy-MM-dd
     *
     * @param timeStamp 时间戳
     * @return
     */
    public static String formatDate(long timeStamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(timeStamp * 1000);
        return date;
    }

    /**
     * 得到时间  HH:mm:ss
     *
     * @param timeStamp 时间戳
     * @return
     */
    public static String getTime(long timeStamp) {
        String time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sdf.format(timeStamp * 1000);
        String[] split = date.split("\\s");
        if (split.length > 1) {
            time = split[1];
        }
        return time;
    }

    /**
     * 得到时间  HH:mm
     *
     * @param timeStamp 时间戳
     * @return
     */
    public static String getTimeHM(long timeStamp) {
        String time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sdf.format(timeStamp);
        String[] split = date.split("\\s");
        if (split.length > 1) {
            time = split[1];
        }
        return time;
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp
     * @return
     */
    public static String convertTimeToFormat(long timeStamp) {
        long curTime = System.currentTimeMillis() / (long) 1000;
        long time = curTime - timeStamp;

        if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600) {
            return time / 60 + "分钟前";
        } else if (time >= 3600 && time < 3600 * 24) {
            return time / 3600 + "小时前";
//            return getTimeHM(timeStamp);
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 7) {
            return time / 3600 / 24 + "天前";
        } else if (time >= 3600 * 24 * 7 && time < 3600 * 24 * 28) {
            return time / 3600 / 24 / 7 + "周前";
        } else if (time >= 3600 * 24 * 28 && time < 3600 * 24 * 30) {
            return 3 + "周前";
        } else if (time >= 3600 * 24 * 30 && time < 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 + "个月前";
        } else if (time >= 3600 * 24 * 30 * 12) {
            return time / 3600 / 24 / 30 / 12 + "年前";
        } else {
            return "刚刚";
        }
    }


    /**
     * 将一个时间戳转换成提示性时间字符串，如刚刚，1秒前
     *
     * @param timeStamp
     * @return
     */
    public static String convertTimeToFormatSmartCourse(long timeStamp) {
        long curTime = System.currentTimeMillis();
        long time = curTime - timeStamp;

        String timeString = getTimeString(timeStamp);
        String curTimeString = getTodayDate();

        if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600) {
            return time / 60 + "分钟前";
        } else if (timeString.substring(0, 10).equals(curTimeString)) {
            return getTimeHM(timeStamp);
        } else if (timeString.substring(0, 4).equals(curTimeString.substring(0, 4))) {
            return getTimeMD(timeStamp);
        } else {
            return timeString.substring(0, 4) + "年" + getTimeMD(timeStamp);
        }
    }


    /**
     * 得到时间  *月*日
     *
     * @param timeStamp 时间戳
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getTimeMD(long timeStamp) {
        String time = null;
        SimpleDateFormat sdf = null;

        Date date = new Date(timeStamp);
        if (date.getMonth() < 10 && date.getDay() > 9) {
            sdf = new SimpleDateFormat("M月dd日");
        } else if (date.getMonth() < 10 && date.getDay() <= 9) {
            sdf = new SimpleDateFormat("M月d日");
        } else if (date.getMonth() >= 10 && date.getDay() <= 9) {
            sdf = new SimpleDateFormat("MM月d日");
        } else if (date.getMonth() >= 10 && date.getDay() > 9) {
            sdf = new SimpleDateFormat("MM月dd日");
        }
        time = sdf.format(timeStamp);
        return time;
    }

    public static String getTimeY(long timeStamp) {
        String time = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        time = sdf.format(timeStamp / 1000);
        return time;
    }


    public static String getWeek(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
        String week = sdf.format(date);
        return week;
    }


    public static String getTimeString(long dateMillis) {

        long curTime = System.currentTimeMillis();
        dateMillis = dateMillis * 1000;
        long time = (curTime - dateMillis) / (long) 1000;
        Date date = new Date(dateMillis);
        String time1 = getTimeY(dateMillis).substring(0, 4);
        String time2 = getTimeY(curTime).substring(0, 4);

        if (!Objects.equals(time1, time2)) {
            return getTimeY(dateMillis);
        } else if (time < 60 && time >= 0) {
            return "刚刚";
        } else if (time >= 60 && time < 3600) {
            return time / 60 + "分钟前";
        } else if (time >= 3600 && time < 3600 * 24) {
            return getTimeHM(dateMillis);
        } else if (time >= 3600 * 24 && time < 3600 * 24 * 2) {
            return "昨天";
        } else if (time >= 3600 * 24 * 2 && time < 3600 * 24 * 7) {
            return getWeek(date);
        } else if (time >= 3600 * 24 * 7 && time < 3600 * 24 * 30 * 12) {
            return getTimeMD(dateMillis);
        } else if (time >= 3600 * 24 * 30 * 12) {
            return getTimeY(dateMillis);
        } else {
            return "刚刚";
        }
    }

    /**
     * 将一个时间戳转换成提示性时间字符串，(多少分钟)
     *
     * @param timeStamp
     * @return
     */
    public static String timeStampToFormat(long timeStamp) {
        long curTime = System.currentTimeMillis() / (long) 1000;
        long time = curTime - timeStamp;
        return time / 60 + "";
    }

    /**
     * 将一个时间字符串转换成时间戳
     *
     * @param stringTime
     * @return
     */
    public static Long stringToTimeStamp(String stringTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        return sdf.parse(stringTime).getTime();
    }

}